<template>
  <div class="home">
  </div>
</template>

<script>
  // @ is an alias to /src

  export default {
    name: 'home',
    components: {},
    methods: {
      openFileHandler: function () {
        const {dialog} = require("electron").remote;
        dialog.showOpenDialog({properties: ['openFile', 'openDirectory']}).then((value => {
          console.log(value.filePaths);
          if (value.filePaths.length > 0) {
            this.currentRoot = value.filePaths[0];
            let split = value.filePaths[0].split("/");
            let data = {
              path: "",
              type: "dir",
              label: split[split.length - 1].toUpperCase(),
              children: []
            };
            this.menu.label = split[split.length - 1];
            let filePath = value.filePaths[0];
            console.log("open folder ===> {}", value.filePaths[0]);
            const path = require("path");
            const fs = require("fs");
            fs.readdir(filePath, function (err, files) {
              let dirs = [];
              (function iterator(i) {
                if (i === files.length) {
                  data.children = dirs;
                  return;
                }
                fs.stat(path.join(filePath, files[i]), function (err, data) {
                  if (data.isFile()) {
                    // dirs.push(files[i]);
                    // this.menu.children
                    let path = files[i];
                    let split = path.split("/");
                    dirs.push({"type": "file", "path": path, "label": split[split.length - 1]})
                  }
                  if (data.isDirectory()) {
                    let path = files[i];
                    let split = path.split("/");
                    dirs.push({"type": "dir", "path": path, "label": split[split.length - 1]})
                  }
                  iterator(i + 1);
                });
              })(0);
            });
            this.menu.push(data)
          }
        }));
      }
    }
  }
</script>
